#include "Graphics.h"
#include"Game.h"
#ifdef _DEBUG
#ifndef HR

#define HR(x)							\
		{								\
		HRESULT hr = x;					\
		if (FAILED(hr))					\
			{							\
			DXTRACE_ERR(#x, hr);		\
			DebugBreak();				\
			}							\
		}
#endif
#else
#ifndef HR
#define HR(x) x;
#endif
#endif

//init D3DXMeshInfo to Null
D3DXMeshInfo::D3DXMeshInfo()
{
	msz_fileName=NULL;
	mp_pieceMaterial=NULL;
	mp_pieceTexture=NULL;
	m_bufPieceMaterial=NULL;
	mdw_pieceNumMaterials=NULL;
	m_meshPiece=NULL;
	D3DXMatrixIdentity(&m_pieceMatRotate);
	D3DXMatrixIdentity(&m_pieceMatScale);
	D3DXMatrixIdentity(&m_pieceMatTranslate);

}
void D3DXMeshInfo::loadMaterials(LPDIRECT3DDEVICE9 &a_dev)
{
	//loads the materials of the piece
	m_pieceTempMaterials =(D3DXMATERIAL*)m_bufPieceMaterial->GetBufferPointer();


	// copy of new something
	mp_pieceMaterial = new D3DMATERIAL9[mdw_pieceNumMaterials];
	mp_pieceTexture = new LPDIRECT3DTEXTURE9[mdw_pieceNumMaterials];

	//TODO with 
	for(DWORD i = 0; i < mdw_pieceNumMaterials; i++)			 // for each material...
	{
		mp_pieceMaterial[i] = m_pieceTempMaterials[i].MatD3D;    // get the material info
		mp_pieceMaterial[i].Ambient =mp_pieceMaterial[i].Diffuse;

		// This will work if we have a x.file for every object
		if(FAILED(D3DXCreateTextureFromFileA(a_dev,
			m_pieceTempMaterials[i].pTextureFilename=msz_texturefileName,
			&mp_pieceTexture[i])))
			mp_pieceTexture[i] = NULL;							 // if there is no texture, set the texture to NULL
	}
}
//this will draw a single piece
void D3DXMeshInfo::drawMesh(LPDIRECT3DDEVICE9 &a_dev)
{
	for(DWORD i = 0; i < mdw_pieceNumMaterials; i++)    // loop through each subset
	{
		a_dev->SetMaterial(&mp_pieceMaterial[i]);		// set the material for the subset

		if(mp_pieceTexture[i] != NULL)					// set texture if necessary
			a_dev->SetTexture(0, mp_pieceTexture[i]);   

		m_meshPiece->DrawSubset(i);						// draw the subset
	}
}
Graphics::Graphics()
	: m_pD3DDevice(NULL)
	, m_hWnd(NULL)
	, m_bWindowed(true)
	, m_bInitialized(false)
{}

bool Graphics::Init(HWND hWnd)
{
	if(!SetupDevice(hWnd))
		return false;
	if(!SetupCamera())
		return false;
	if(!SetupFont())
		return false;
	if(!SetupMeshes())
		return false;
	if(!SetupLight())
		return false;
	if(!SetupMaterials())
		return false;

	// setup sprite
	D3DXCreateSprite(m_pD3DDevice, &m_pD3DSprite);
	//Setup menu sprite

	//Get Menu Ready, but it's apparently not working straight.
	if(FAILED(D3DXCreateTextureFromFile(m_pD3DDevice,"./Resources/menu.png",&m_menuTex)))
	{
		MessageBox(NULL,"Menu Image was not Loaded.","ERROR!",MB_OK);
	}
	Menu::Engine()->Init(set(0,0,0),set(SCREEN_WIDTH/2.0f,SCREEN_HEIGHT/2.0f,0.0f),set(.8f,.6f,0.0f),"./Resources/menu.png");
	m_menuTex->GetLevelDesc(0,&m_menuDesc);
	Menu::Engine()->setCenter(set(m_menuDesc.Width/2.0f,m_menuDesc.Height/2.0f,0.0f));

	m_sPause.m_position=set(SCREEN_WIDTH/2.0f,SCREEN_HEIGHT/2.0f,0);
	m_sPause.m_scale=set(.8f,.6f,0.0f);
	D3DXCreateTextureFromFile(m_pD3DDevice,"./Resources/NextTurn.png",&m_sPause.m_texture);
	m_sPause.m_texture->GetLevelDesc(0,&m_sPause.m_desc);
	m_sPause.m_center=set(m_sPause.m_desc.Width/2.0f,m_sPause.m_desc.Height/2.0f,0);


	m_rCardLoc[0].left=1;    m_rCardLoc[0].top=2;    m_rCardLoc[0].right=98;   m_rCardLoc[0].bottom=190;
	m_rCardLoc[1].left=126;  m_rCardLoc[1].top=4;    m_rCardLoc[1].right=221;  m_rCardLoc[1].bottom=191;
	m_rCardLoc[2].left=244;  m_rCardLoc[2].top=2;    m_rCardLoc[2].right=343;  m_rCardLoc[2].bottom=191;
	m_rCardLoc[3].left=364;  m_rCardLoc[3].top=0;    m_rCardLoc[3].right=465;  m_rCardLoc[3].bottom=191;
	m_rCardLoc[4].left=487;  m_rCardLoc[4].top=0;    m_rCardLoc[4].right=584;  m_rCardLoc[4].bottom=191;
	m_rCardLoc[5].left=611;  m_rCardLoc[5].top=0;    m_rCardLoc[5].right=707;  m_rCardLoc[5].bottom=191;
	m_rCardLoc[6].left=0;    m_rCardLoc[6].top=201;  m_rCardLoc[6].right=101;  m_rCardLoc[6].bottom=392;
	m_rCardLoc[7].left=122;  m_rCardLoc[7].top=201;  m_rCardLoc[7].right=222;  m_rCardLoc[7].bottom=392;
	m_rCardLoc[8].left=242;  m_rCardLoc[8].top=201;  m_rCardLoc[8].right=342;  m_rCardLoc[8].bottom=392;
	m_rCardLoc[9].left=364;  m_rCardLoc[9].top=201;  m_rCardLoc[9].right=464;  m_rCardLoc[9].bottom=392;
	m_rCardLoc[10].left=485; m_rCardLoc[10].top=201; m_rCardLoc[10].right=587; m_rCardLoc[10].bottom=392;
	m_rCardLoc[11].left=608; m_rCardLoc[11].top=201; m_rCardLoc[11].right=708; m_rCardLoc[11].bottom=392;
	m_rCardLoc[12].left=1;   m_rCardLoc[12].top=403; m_rCardLoc[12].right=100; m_rCardLoc[12].bottom=599;
	m_rCardLoc[13].left=122; m_rCardLoc[13].top=403; m_rCardLoc[13].right=223; m_rCardLoc[13].bottom=599;


	//m_rCardLoc[SORRY_CARD]


	m_bInitialized = true;
	return m_bInitialized;
}

// use a D3DObject to create the device we will use from here on out
bool Graphics::SetupDevice(HWND hWnd)
{
	LPDIRECT3D9 pD3DObject = NULL;
	pD3DObject = Direct3DCreate9(D3D_SDK_VERSION);		// create the d3dobject

	m_hWnd = hWnd;										// make a copy of hWnd for the Graphics class

	RECT rClientRect;
	if(!::GetClientRect(m_hWnd, &rClientRect))
	{
		throw "GetClientRect() in Graphics::SetupDevice() failed";
		return false;
	}

	// grab screen dimensions from the window
	m_dimScreen.width = (int)(rClientRect.right - rClientRect.left);
	m_dimScreen.height = (int)(rClientRect.bottom - rClientRect.top);

	D3DCAPS9 D3DCaps;

	HR(pD3DObject->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &D3DCaps));

	DWORD dwBehaviorFlags = 0;	

	// determine what type of vertex processing to use
	if(D3DCaps.VertexProcessingCaps != 0)
		dwBehaviorFlags |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
	else
		dwBehaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;

	//setup pp
	m_D3Dpp.hDeviceWindow			= m_hWnd;							// what window the device will use
	m_D3Dpp.Windowed				= m_bWindowed;						// whether or not the window is fullscreen or not
	m_D3Dpp.EnableAutoDepthStencil  = TRUE;								// tells the device to manage depth buffering
	m_D3Dpp.AutoDepthStencilFormat	= D3DFMT_D16;						// the type of depth buffering
	m_D3Dpp.BackBufferCount			= 1;								// no need for more than one back buffer
	m_D3Dpp.BackBufferFormat		= D3DFMT_X8R8G8B8;
	m_D3Dpp.BackBufferWidth			= SCREEN_WIDTH;						// the dimensions should match the front screen (front buffer)
	m_D3Dpp.BackBufferHeight		= SCREEN_HEIGHT;
	m_D3Dpp.SwapEffect				= D3DSWAPEFFECT_DISCARD;			// after displaying a frame, delete it
	m_D3Dpp.Flags					= D3DPRESENTFLAG_DISCARD_DEPTHSTENCIL;
	m_D3Dpp.MultiSampleQuality		= 0;
	m_D3Dpp.MultiSampleType			= D3DMULTISAMPLE_NONE;
	m_D3Dpp.PresentationInterval    = D3DPRESENT_INTERVAL_IMMEDIATE;
	// now create a device using all the above settings for us to use
	pD3DObject->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hWnd, dwBehaviorFlags, &m_D3Dpp, &m_pD3DDevice);
	pD3DObject->Release();												// we don't need the object after this, release it from memory

	if(m_pD3DDevice == NULL)											// if the device was not set, return false
		return false;
	m_pD3DDevice->SetRenderState(D3DRS_LIGHTING, TRUE);					// turn on the 3D lighting
	m_pD3DDevice->SetRenderState(D3DRS_ZENABLE, TRUE);					// turn on the z-buffer
	m_pD3DDevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(150, 150, 150)); 
	m_pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);			// both sides of the triangles

	m_pD3DDevice->SetSamplerState(0, D3DSAMP_MAXANISOTROPY, 8);					// anisotropic level
	m_pD3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC);   // minification
	m_pD3DDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);		// magnification
	m_pD3DDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);		// mipmap
	return true;
}

bool Graphics::SetupCamera()
{

	m_Camera.eyePos = D3DXVECTOR3(1500, 500, 0);
	m_Camera.lookAt = D3DXVECTOR3(0, 0, 0);
	m_Camera.upVec =  D3DXVECTOR3(0, 1, 0);

	D3DXMatrixPerspectiveFovLH(&m_matPerspective, D3DXToRadian(22), 
		(float)(SCREEN_WIDTH / SCREEN_HEIGHT), 1.0f, 10000.0f);
	D3DXMatrixLookAtLH(&m_matView, &m_Camera.eyePos, &m_Camera.lookAt, &m_Camera.upVec);

	m_pD3DDevice->SetTransform(D3DTS_PROJECTION, &m_matPerspective);
	m_pD3DDevice->SetTransform(D3DTS_VIEW, &m_matView);

	return true;
}

bool Graphics::SetupFont()
{
	m_rTextBox.left = 0;
	m_rTextBox.right = 20;
	m_rTextBox.top = 0;
	m_rTextBox.top = 20;

	D3DXCreateFont(m_pD3DDevice, 18, 0, NULL, 0, false,		// initialize the font handler
		DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY,
		DEFAULT_PITCH | FF_DONTCARE, TEXT("Times New Roman"),
		&m_pD3DFont);

	if(NULL == m_pD3DFont)
		return false;

	return true;
}

bool Graphics::SetupMeshes()
{
	// names of the .x files for the consoles
	DrawInfo[FS3_MESH].msz_fileName				="./Resources/FS3Shape.x";
	DrawInfo[YBOX_MESH].msz_fileName			="./Resources/yBox.x";
	DrawInfo[PC_MESH].msz_fileName				="./Resources/pc.x";
	DrawInfo[WEE_MESH].msz_fileName				="./Resources/wee.x";
	DrawInfo[BOARD_MESH].msz_fileName			="./Resources/BoardT2.x";

	//the name of where you store the textures
	DrawInfo[WEE_MESH].msz_texturefileName		="./Resources/WeeTexture.png";
	DrawInfo[FS3_MESH].msz_texturefileName		="./Resources/FS3Tex.png";
	DrawInfo[YBOX_MESH].msz_texturefileName		="./Resources/ybox180.png";
	DrawInfo[PC_MESH].msz_texturefileName		="./Resources/PCTex.png";
	DrawInfo[BOARD_MESH].msz_texturefileName	="./Resources/BoardTex.png";

	// the name of where the card textures are located
	m_mszCardTextureFileName[SORRY_CARD]		= "./Resources/Cards/sorry.png";
	m_mszCardTextureFileName[1]					= "./Resources/Cards/one.png";
	m_mszCardTextureFileName[2]					= "./Resources/Cards/two.png";
	m_mszCardTextureFileName[3]					= "./Resources/Cards/three.png";
	m_mszCardTextureFileName[4]					= "./Resources/Cards/four.png";
	m_mszCardTextureFileName[5]					= "./Resources/Cards/five.png";
	m_mszCardTextureFileName[6]					= "./Resources/Cards/six.png";
	m_mszCardTextureFileName[7]					= "./Resources/Cards/seven.png";
	m_mszCardTextureFileName[8]					= "./Resources/Cards/eight.png";
	m_mszCardTextureFileName[9]					= "./Resources/Cards/nine.png";
	m_mszCardTextureFileName[10]				= "./Resources/Cards/ten.png";
	m_mszCardTextureFileName[11]				= "./Resources/Cards/eleven.png";
	m_mszCardTextureFileName[12]				= "./Resources/Cards/twelve.png";
	m_mszCardTextureFileName[WEE_SPECIAL_CARD]		= "./Resources/Cards/weeSpecial.png";
	m_mszCardTextureFileName[PC_SPECIAL_CARD]		= "./Resources/Cards/pcSpecial.png";
	m_mszCardTextureFileName[YBOX_SPECIAL_CARD]		= "./Resources/Cards/yboxSpecial.png";
	m_mszCardTextureFileName[FUNSTATION_SPECIAL_CARD]		= "./Resources/Cards/funstationSpecial.png";

	// load textures for cards
	// UNCOMMENT THIS WHEN CARD TEXTURES HAVE BEEN MADE
	for(int i = 0; i < g_inumOfDifferentCards; i++)
		D3DXCreateTextureFromFile(m_pD3DDevice, m_mszCardTextureFileName[i], &m_CardTextures[i]);


	//loads the meshes for consoles and devices in meshPiece
	for(int i=0; i<g_inumDiffMeshes;i++)
		LoadMeshFile(DrawInfo[i]);

	return true;
}

bool Graphics::SetupLight()		// setup a very basic D3DLIGHT9 light
{

	m_pD3DDevice->SetRenderState(D3DRS_SPECULARENABLE, TRUE);
	m_pD3DDevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(60, 60, 60));

	ZeroMemory(&m_DefaultLight, sizeof(D3DLIGHT9));

	m_DefaultLight.Type = D3DLIGHT_POINT;
	m_DefaultLight.Ambient = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
	m_DefaultLight.Diffuse = D3DXCOLOR(0.1f, 0.1f, 0.1f, 1);		// color of the light
	m_DefaultLight.Specular = D3DXCOLOR(0.5f, 0.5f, 0.5f, 1);
	m_DefaultLight.Position.x = 0;	
	m_DefaultLight.Position.y = 40;	
	m_DefaultLight.Position.z = 70;
	D3DXVec3Normalize((D3DXVECTOR3*)&m_DefaultLight.Direction, (D3DXVECTOR3*)&m_DefaultLight.Position);
	m_DefaultLight.Range = 1000.0f;
	m_DefaultLight.Attenuation0 = 0;
	m_DefaultLight.Attenuation1 = 0.05f;
	m_DefaultLight.Attenuation2 = 0.0f;

	m_PieceLight.Type = D3DLIGHT_POINT;
	m_PieceLight.Ambient = D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f);
	m_PieceLight.Diffuse = D3DXCOLOR(0.0f, 0.0f, 0.1f, 1);		// color of the light
	m_PieceLight.Specular = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1);
	m_PieceLight.Position.x = 0;	
	m_PieceLight.Position.y = 40;	
	m_PieceLight.Position.z = 70;
	D3DXVec3Normalize((D3DXVECTOR3*)&m_PieceLight.Direction, (D3DXVECTOR3*)&m_PieceLight.Position);
	m_PieceLight.Range = 40.0f;
	m_PieceLight.Attenuation0 = 0;
	m_PieceLight.Attenuation1 = 0.05f;
	m_PieceLight.Attenuation2 = 0.0f;

	m_pD3DDevice->SetLight(0, &m_DefaultLight);

	for(int i = 0; i < 3; i++)
		m_pD3DDevice->LightEnable(i, true);

	return true;
}

bool Graphics::SetupMaterials()
{
	m_pD3DDevice->SetRenderState(D3DRS_SPECULARMATERIALSOURCE, D3DMCS_MATERIAL);

	return true;
}


void Graphics::RenderFont(BaseObject* pObj)
{
	std::stringstream m_FontStream;
	RECT m_rTextBox;
	m_rTextBox.top = m_rTextBox.left = 0;
	m_rTextBox.bottom = m_rTextBox.right = 100;

	m_FontStream << "X : " << pObj->pos.x << "\n";
	m_FontStream << "Y : " << pObj->pos.y << "\n";
	m_FontStream << "Z : " << pObj->pos.z << "\n";

	m_pD3DFont->DrawTextA(NULL, m_FontStream.str().c_str() ,
		m_FontStream.str().size(), &m_rTextBox,
		DT_LEFT | DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 255, 255));

}

void Graphics::MoveCameraTo(D3DXVECTOR3 newPos)
{
	m_Camera.eyePos = newPos;
	UpdateCamera();
}

void Graphics::UpdateCamera()
{
	
	D3DXMatrixLookAtLH(&m_matView, &m_Camera.eyePos, &m_Camera.lookAt, &m_Camera.upVec);

	m_pD3DDevice->SetTransform(D3DTS_PROJECTION, &m_matPerspective);
	m_pD3DDevice->SetTransform(D3DTS_VIEW, &m_matView);
}

// to be called every game loop, clearing the back buffer, and drawing the next frame
bool Graphics::RenderGame(Vector<BaseObject*> vToDraw, bool bs)
{
	if(!m_bInitialized)
	{
		MessageBox(0, "Graphics is being used without being initialized, shutting down...", 0, 0);
		return false;
	}

	// clear the entire back buffer and fill the screen to be dark grey
	m_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB( 10, 10, 10), 1.0f, 0);

	if(DInput::Engine()->keyHeld(DIK_LSHIFT))
	{
		// camera left-right movement
		if(DInput::Engine()->keyHeld(DIK_W))
			m_Camera.lookAt.y +=1;
		if(DInput::Engine()->keyHeld(DIK_S))
			m_Camera.eyePos.y += 1;
		if(DInput::Engine()->keyHeld(DIK_A))
			m_Camera.eyePos.x -= 1;
		if(DInput::Engine()->keyHeld(DIK_D))
			m_Camera.eyePos.x += 1;

		// camera depth
		if(DInput::Engine()->keyHeld(DIK_UP))
			m_Camera.eyePos.z += 1;
		if(DInput::Engine()->keyHeld(DIK_DOWN))
			m_Camera.eyePos.z -= 1;
	}

	m_pD3DDevice->BeginScene();

	if(bs)
	{
		for(int i = 0; i < vToDraw.sizeOf(); i++)
		{
			D3DXMatrixScaling(&m_matScale, 
				vToDraw[i]->scale.x,
				vToDraw[i]->scale.y,
				vToDraw[i]->scale.z);

			D3DXMatrixTranslation(&m_matTrans,
				vToDraw[i]->pos.x,
				vToDraw[i]->pos.y,
				vToDraw[i]->pos.z);

			D3DXMatrixRotationYawPitchRoll(&m_matRot, 
				D3DXToRadian(vToDraw[i]->rotate.x),
				D3DXToRadian(vToDraw[i]->rotate.y),
				D3DXToRadian(vToDraw[i]->rotate.z));

			m_pD3DDevice->SetTransform(D3DTS_WORLD, 
				&(m_matRot * m_matScale * m_matTrans)); 

			DrawInfo[vToDraw[i]->m_iMeshIndex].drawMesh(m_pD3DDevice);
		//	if(vToDraw[i]->DEBUGGING)
		//	{
				// display the position coordinates for that piece
			//RenderFont(vToDraw[0]);

				// have the camera follow that piece
			/*	m_Camera.eyePos.x = vToDraw[i]->pos.x;
				m_Camera.eyePos.z = vToDraw[i]->pos.z;

				m_Camera.lookAt.x = vToDraw[i]->pos.x-1;
				m_Camera.lookAt.z = vToDraw[i]->pos.z;*/
	//	}

			//Game::Engine()->m_pBoard->m_home[g_inumPlayers];
			RenderFont(vToDraw[13]);
		}
	}
	char clickMsg[256];
	POINT Click;
	float x;
	float y;
	GetCursorPos(&Click);									//Gets the mouse position
		ScreenToClient(m_hWnd,&Click);							//Converts screen to local of mouse coordinates
		x=(float)Click.x;										//saves data
		y=(float)Click.y;	
	/*End Temporary Values*/
	D3DXVECTOR3 temp; //temp variable for XYZ data handling

	//if(DInput::Engine()->mouseButtonDown(LEFT_MOUSE_BUTTON) && !PtInRect(&m_rCardLoc[0],Click))	//Left click
	//{
	//										//saves data

	//	sprintf_s(clickMsg,sizeof(clickMsg),					//Prints the data into a char array
	//		"X,Y: (%.0f,%.0f)",x,y);		
	//		MessageBox(m_hWnd,clickMsg,"Coordinates",MB_OK);		//Present data
	//}
	//D3DXVECTOR3 temp;
	UpdateCamera();
	m_pD3DSprite->Begin(D3DXSPRITE_ALPHABLEND);
	switch(Game::Engine()->m_eState)
	{
	case CONSOLE_SELECT:

		break;
	case ORDER_SELECT:

		break;
	case CARD_SELECT:
		{
			//temps for the row count and col count
			int rowCount=0,colCount=0;
			float placeOnY=0.0f;
			//the offset for the cards
			XYZ offset;

			offset.x=124.0f;
			offset.y=214.0f;

			D3DXMatrixIdentity(&m_matWorld);
			//found a decent scaling factor
			D3DXMatrixScaling(&m_matScale,.4,.4 ,0);
			
			//draws all the cards
			for(int i=0;i<g_inumOfPlayerCards;i++)
			{
				if(rowCount>5)
				{
					//distance from one to the next
					placeOnY+=offset.y;
					colCount=0;
					rowCount=0;
				}
				//translates them in a colum
				D3DXMatrixTranslation(&m_matTrans,0+(offset.x*colCount),0.0f+placeOnY,1.0f);
				m_pD3DSprite->SetTransform(&(m_matScale*m_matTrans));
				//thinkin make array of players cards and pass it in the i to draw their cards 
				if(Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_playersCards[i]!=-1)
					m_pD3DSprite->Draw(m_CardTextures[Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_playersCards[i]],0,0,NULL,D3DCOLOR_XRGB(255,255,255));
				
				
				m_iCardNum[i]=Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_playersCards[i];
				
				colCount++;
				rowCount++;
			}
		
			//goes through each of the players and checks their cards
			for (int a=0;a<g_inumPlayers;a++)
			{
			//	if(Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].checkCards())
					//MessageBox(m_hWnd,"Empty","kay",MB_OK);
			}
			
			int count=0;
			for (int i=0;i<g_inumOfPlayerCards;i++)
				if (DInput::Engine()->mouseButtonDown(1) && PtInRect(&m_rCardLoc[i],Click))
				{
					Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_cardBeingUsed=m_iCardNum[i];
						Game::Engine()->m_eState=PIECE_SELECT;
							Game::Engine()->m_bDrawBoard=true;
					
					Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_cardsUsed+=1;
					//break;
					//This code needs some fixing, it needs to check against all of them and if they are all set to true, then a 1 or 2 may work.
					/*if(Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[1]->m_home==true &&
						Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_cardBeingUsed==1 ||
						Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_cardBeingUsed==2)
						{
							count++;
							if(count>=3)
							{
							Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_playersCards[i]=-1;
							Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_playersCards[i]=-1;
						
							}
							
						}
						
					count=0;*/
					  
				}


		}
		break;
	case PIECE_SELECT:
		{
			//light should move to the piece you want 
			if(DInput::Engine()->keyPressed(DIK_LEFT))
			{
				Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceToMove--;
				
			}
			if(DInput::Engine()->keyPressed(DIK_RIGHT))
			{
				Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceToMove++;
				
			}
			//if piece to move is equal to num pieces minus one
			if(Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceToMove>=g_inumPieces)
			{
				Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceToMove=g_inumPieces-1;
			}
			//if num of pieces goes under 0 make it zero
			if(Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceToMove<=0)
			{
				Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceToMove=0;
			}

			m_Camera.lookAt.x=Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceToMove]->pos.x;
			m_Camera.lookAt.y=Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceToMove]->pos.y;
			m_Camera.lookAt.z=Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceToMove]->pos.z;

			m_Camera.eyePos=(D3DXVECTOR3&)Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceToMove]->pos+(D3DXVECTOR3&)Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[Game::Engine()->m_players->m_pieceToMove]->getPos();

			
		//	m_Camera.eyePos.y=Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[Game::Engine()->m_players->m_pieceToMove]->pos.y+200;
		//	m_Camera.eyePos.z=Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[Game::Engine()->m_players->m_pieceToMove]->pos.z+400;

			/*m_Camera.eyePos.x=Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[Game::Engine()->m_players->m_pieceToMove]->pos.x;
			m_Camera.eyePos.y=Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[Game::Engine()->m_players->m_pieceToMove]->pos.y+200;
			m_Camera.eyePos.z=Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[Game::Engine()->m_players->m_pieceToMove]->pos.z+400;*/
			//moves the light to the piece to move
				m_PieceLight.Position.x=Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceToMove]->pos.x;
				m_PieceLight.Position.y=Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceToMove]->pos.y;
				m_PieceLight.Position.z=Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceToMove]->pos.z;
			m_pD3DDevice->SetLight(2, &m_PieceLight);
			//m_pD3DDevice->SetLight(0, &m_DefaultLight);

			//if you press enter move the piece
			if(DInput::Engine()->keyHeld(DIK_RETURN))
			{
					//so it shows moving through the pieces
			//Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_moveDone=Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_cardBeingUsed;
				Game::Engine()->m_eState=PIECE_MOVE;
			}
		}
		break;
	case PIECE_MOVE:
		{
			m_Camera.lookAt.x=Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceToMove]->pos.x;
			m_Camera.lookAt.y=Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceToMove]->pos.y;
			m_Camera.lookAt.z=Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceToMove]->pos.z;

			m_Camera.eyePos=(D3DXVECTOR3&)Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceToMove]->pos+(D3DXVECTOR3&)Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[Game::Engine()->m_players->m_pieceToMove]->getPos();

			
			//if cant remove get rid of a card
			//if you can move move
			m_PieceLight.Position.x=Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceToMove]->pos.x;
				m_PieceLight.Position.y=Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceToMove]->pos.y;
				m_PieceLight.Position.z=Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceToMove]->pos.z;
			m_pD3DDevice->SetLight(0, &m_PieceLight);
			
			
			//Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceToMove]->pos=Game::Engine()->m_pBoard->m_boardSpots[Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceToMove]->m_iSquareIndex+Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_moveDone].pos;
			//Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceToMove]->m_iSquareIndex++;
			//Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_moveDone--;
			////when done adds
			//if(Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_moveDone==0)
			//{
				if(Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].MovePiece(Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceToMove,   Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_cardBeingUsed))
				Game::Engine()->m_eState=PLAYER_SWITCH;

				//if(Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].MovePiece(Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceToMove,   Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_cardBeingUsed))
				//Game::Engine()->m_eState=PLAYER_SWITCH;
			//}
			//until the move piece has the function move added
			//Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[Game::Engine()->m_players->m_pieceToMove]->pos  =  Game::Engine()->m_pBoard->m_boardSpots[Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_cardBeingUsed  +Game::Engine()->m_players[Game::Engine()->whoseTurnIsIt].m_pieceInfo[Game::Engine()->m_players->m_pieceToMove]->m_iSquareIndex].pos;
			//
			
		}
		break;
	case PLAYER_SWITCH:
		{
			
			// draws the menu image
			D3DXMatrixIdentity(&m_matWorld);
			D3DXMatrixTranslation(&m_matTrans,m_sPause.m_position.x,m_sPause.m_position.y,1.0f);
			D3DXMatrixScaling(&m_matScale,m_sPause.m_scale.x,m_sPause.m_scale.y ,0);
			m_pD3DSprite->SetTransform(&(m_matScale*m_matTrans));
			temp.x=Menu::Engine()->retCenter().x;//set x
			temp.y=Menu::Engine()->retCenter().y;//set y
			temp.z=Menu::Engine()->retCenter().z;//set z
			m_pD3DSprite->Draw(m_sPause.m_texture,0,&temp,NULL,D3DCOLOR_XRGB(255,255,255));
		}

		break;
	case END:

		break;
	case PAUSE:
		break;
	}

	if(bs)
	{
		//// draws the menu image
		//D3DXMatrixIdentity(&m_matWorld);
		//D3DXMatrixTranslation(&m_matTrans,m_sPause.m_position.x,m_sPause.m_position.y,1.0f);
		//D3DXMatrixScaling(&m_matScale,m_sPause.m_scale.x,m_sPause.m_scale.y ,0);
		//m_pD3DSprite->SetTransform(&(m_matScale*m_matTrans));
		//temp.x=Menu::Engine()->retCenter().x;//set x
		//temp.y=Menu::Engine()->retCenter().y;//set y
		//temp.z=Menu::Engine()->retCenter().z;//set z
		//m_pD3DSprite->Draw(m_sPause.m_texture,0,&temp,NULL,D3DCOLOR_XRGB(255,255,255));
	}

	m_pD3DSprite->End();
	m_pD3DDevice->EndScene();
	m_pD3DDevice->Present(NULL, NULL, NULL, NULL);		// present the completed frame to the screen


	return true;
}

bool Graphics::RenderMenu()
{
	if(!m_bInitialized)
	{
		MessageBox(0, "Graphics is being used without being initialized, shutting down...", 0, 0);
		return false;
	}

	// clear the entire back buffer and fill the screen to be dark grey
	m_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 10, 10, 10), 1.0f, 0);
	m_pD3DDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB( 10, 10, 10), 1.0f, 0);

	/*Temporary Values*/
	char clickMsg[256];
	POINT Click;
	float x;
	float y;
	/*End Temporary Values*/
	D3DXVECTOR3 temp; //temp variable for XYZ data handling

	//if(DInput::Engine()->mouseButtonDown(LEFT_MOUSE_BUTTON)&&eMainState ==MENU)	//Left click
	//{
	//	GetCursorPos(&Click);									//Gets the mouse position
	//	ScreenToClient(m_hWnd,&Click);							//Converts screen to local of mouse coordinates
	//	x=(float)Click.x;										//saves data
	//	y=(float)Click.y;										//saves data

	//	sprintf_s(clickMsg,sizeof(clickMsg),					//Prints the data into a char array
	//		"X,Y: (%.0f,%.0f)",x,y);		
	//		MessageBox(m_hWnd,clickMsg,"Coordinates",MB_OK);		//Present data
	//}

	m_pD3DDevice->BeginScene();

	m_pD3DSprite->Begin(D3DXSPRITE_ALPHABLEND);

	// draws the menu image
	D3DXMatrixIdentity(&m_matWorld);
	D3DXMatrixTranslation(&m_matTrans,Menu::Engine()->retPos().x,Menu::Engine()->retPos().y,1.0f);
	D3DXMatrixScaling(&m_matScale,Menu::Engine()->retScale().x ,Menu::Engine()->retScale().y ,0);
	m_pD3DSprite->SetTransform(&(m_matScale*m_matTrans));
	temp.x=Menu::Engine()->retCenter().x;//set x
	temp.y=Menu::Engine()->retCenter().y;//set y
	temp.z=Menu::Engine()->retCenter().z;//set z
	m_pD3DSprite->Draw(m_menuTex,0,&temp,NULL,D3DCOLOR_XRGB(255,255,255));

	m_pD3DSprite->End();

	m_pD3DDevice->EndScene();

	m_pD3DDevice->Present(NULL, NULL, NULL, NULL);		// present the completed frame to the screen


	return true;
}

//@D3DXMeshInfo- holds all the information of the mesh
void Graphics::LoadMeshFile(D3DXMeshInfo &meshInfo)
{

	//make sure you have a mesh to draw and a valid file name
	assert((meshInfo.msz_fileName != NULL));

	/* Load the file into the mesh */
	HR(D3DXLoadMeshFromX(meshInfo.msz_fileName, D3DXMESH_MANAGED, m_pD3DDevice, NULL,
		&meshInfo.m_bufPieceMaterial,NULL, &meshInfo.mdw_pieceNumMaterials, &meshInfo.m_meshPiece));
	DWORD dError = ::GetLastError();

	meshInfo.loadMaterials(m_pD3DDevice);
}


Graphics::~Graphics()
{
	//release the objects for the meshes
	for(int i=0;i<g_inumPlayers;i++)
		DrawInfo[i].m_meshPiece->Release();
	if(NULL != m_pD3DDevice)		// if the device was set up properly
		m_pD3DDevice->Release();
}